<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var arr = [
      {
        id: "01",
        pid: "",
        name: "老王",
        children: []
      },
      {
        id: "02",
        pid: "01",
        name: "小张",
        children: []
      },
      {
        id: "03",
        pid: "02",
        name: "小xx",
        children: []
      }
    ]
    function arrTotree(list, rootValue) {
      const treeArr = []
      list.forEach(item => {
        if (item.pid === rootValue) {
          const children = arrTotree(list, item.id)
          if (children.length) {
            item.children = children
          }
          treeArr.push(item)
        }
      })
      return treeArr
    }
    console.log(arrTotree(arr, ''))
  </script>
</body>

</html>